package cn.wjdiankong.levideo.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import cn.wjdiankong.levideo.data.LevideoData;
import cn.wjdiankong.levideo.utils.Logger;

public class Main_DbHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "levideo.db";
    private static final int VERSION = 1;
    private static Main_DbHelper sInstance = null;

    public static synchronized Main_DbHelper getInstance(Context context) {
        if (sInstance == null) {
            synchronized (Main_DbHelper.class){
                if(sInstance == null){
                    sInstance = new Main_DbHelper(context);
                }
            }
        }
        return sInstance;
    }

    public Main_DbHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Logger.log("onCreate");
        LevideoData.onCreateTable(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
        try{
            db.execSQL("DROP TABLE IF EXISTS " + LevideoData.TABLE_NAME);
        }catch (Exception e){
            Logger.log("drop table error:"+ Log.getStackTraceString(e));
        }
        LevideoData.onCreateTable(db);
    }

}